#include "ElectricAreaEffect.h"
#include "../source/GameObjects/Gun/Bullet.h"
#include <Windows.h>
#include "../SGD_Wrappers/CSGD_Direct3D.h"
#include "CreateLightPrimBullet.h"
#include "../SGD_Wrappers/IMessage.h"
#include "../SGD_Wrappers/CSGD_MessageSystem.h"
#include "DestroyEntityMessage.h"

CElectricAreaEffect::CElectricAreaEffect(void)
{
	m_fLifeTime = 1.0f;
	shot = false;

	m_pTarget = nullptr;

}

CElectricAreaEffect::CElectricAreaEffect(int x, int y)
{
	m_fLifeTime = 1.0f;
	shot = false;

	this->SetPosX((float)x);
	this->SetPosY((float)y);

	m_pTarget = nullptr;

}

CElectricAreaEffect::~CElectricAreaEffect(void)
{
}

void CElectricAreaEffect::Update(float fElapsedTime)
{
	m_fLifeTime -= fElapsedTime;
	if( m_fLifeTime <= 0.0f )
	{
		IMessage* d = new CDestroyEntityMessage( this );
		CSGD_MessageSystem::GetInstance()->SendMsg( d );
		d = nullptr;
	}


	if( m_fLifeTime < 0.5f && shot == false )
	{
		if( m_pTarget != nullptr )
		{
			Pos.fX = this->GetPosX();
			Pos.fY = this->GetPosY();

			Tar.fX = m_pTarget->GetPosX();
			Tar.fY = m_pTarget->GetPosY();

			angle = AngleBetweenVectors( Pos, Tar );

			D3DXToRadian(angle);

			IMessage* i = new CreateLightPrimBullet( this );
			CSGD_MessageSystem::GetInstance()->SendMsg( i );
			i = nullptr;


			shot = true;

			IMessage* d = new CDestroyEntityMessage( this );
			CSGD_MessageSystem::GetInstance()->SendMsg( d );
			d = nullptr;

		}
	}
	
}

void CElectricAreaEffect::Render( void )
{

}

bool CElectricAreaEffect::CheckCollision( IEntity* pOther)
{
	int tempWidth, tempHeight, tempArea;

	RECT rSelf = GetRect();
	RECT rOther = pOther->GetRect();
	RECT rOverlap;

	if( pOther->GetType() == ENTITY_PLAYER )
		return false;
	

	if(IntersectRect(&rOverlap, &rSelf, &rOther)==TRUE)
	{
		
		tempWidth  = rOverlap.right - rOverlap.left;
		tempHeight = rOverlap.bottom - rOverlap.top;
		tempArea = tempWidth * tempHeight;

		
			if( RectArea < tempArea )
			{
				RectArea = tempArea;
				

				InterRect.left = rOverlap.left;
				InterRect.right = rOverlap.right;
				InterRect.bottom = rOverlap.bottom;
				InterRect.top = rOverlap.top;

				
				m_pTarget = dynamic_cast<CEntity*>(pOther);

				return true;
			}
	
	}
	return false;
}


float	CElectricAreaEffect::GetAngle( void )
{
	return angle;
}
